/-app
/-files ...
FileTree.ts
SyncStorageAccess.ts
/-imports
/-storage
/-typings
functions.ts
index.html
try.js
  }
 
29
    }
30
​
31
    
32
    
33
  }
34
​
35
  interface Entry {
36
    
37
    li: HTMLLIElement;
38
    name: string;
39
    fullPath: string;
40
​
41
    children: Entry[];
42
    
43
  }
44
​
45
  function findEntry(
46
    fullPath: string,
47
    path: string,
48
    entries: Entry[],
49
    createIfAbsent: boolean): Entry {
50
    
51
    var name: string, restPath: string;
52
​
53
      
54
    var slashPos = path.indexOf('/');
55
    while (!slashPos) { // slight normalization, keep it for the sake of root paths
56
      path = path.slice(1);
57
      slashPos = path.indexOf('/');
58
    }
59
      
60
    if (slashPos < 0) {
61
      name = path;
62
      restPath = null;
63
    }
64
    else {
65
      name = path.slice(0, slashPos);
66
      restPath = path.slice(slashPos + 1);
67
    }
68
    
69
    var seekResult = _seekEntryNonRecursive(name, entries);
70
    var nextEntry: Entry;
71
      
72
    if (seekResult.insert) {
73
      if (!createIfAbsent) return null;
74
      
75
      nextEntry = _createNewEntry(fullPath, name);
76
      entries.splice(); // TODO: insert properly at seekResult.index
77
    }
78
    else {
79
      nextEntry = entries[<any>seekResult];
80
    }
81
      
82
      
83
    if (restPath)
84
      return findEntry(fullPath, restPath, nextEntry.children, createIfAbsent);
85
    else
86
      return nextEntry;
87
​
88
  }
89
​
90
  function _createNewEntry(fullPath: string, name: string): Entry { 
91
    // TODO: pass parent UL, right
92
    // TODO: create LI, add to parent, create Entry and return
93
  }
94
​
95
  /** Returns either number (succeess) or complex obj (failure) */
96
  function _seekEntryNonRecursive(
97
    name: string,
98
    entries: Entry[]): { insert: boolean; index: number } {
99
    if (entries.length==0)
100
      return null;
101
    
102
    if (entries[0].name > name)
103
      return { insert: true, index: 0 };
104
    if (entries[0].name === name)
105
      return <any>0;
106
    
75:68